var roleCode;
var depCode;
$(function(){
	showCondition(true, 0, 0, 1,[]);//加载查询条件
	
	var param = drgUI.setAjaxData(tabId);//获取当前页面 初始参数
	roleCode = $('#roles').text();
	depCode = $('#deps_id').val();
	
	if(roleCode=='科室' || '科室绩效'==roleCode){//临床科主任角色
		
	}else{//当是医政/医保角色时
		$('#'+tabId+' .con-row').css('display','none');
		//如果是  科室指标象限分析--二级科室分析页面，点击“病组结构分析”按钮跳转进来的，则隐藏所有科室组，科室，院区的下拉框，并加载上级页面传递过来的科室信息
		if(param.patientGroupAnalysisPage == 'yizheng'){
			//隐藏所有科室组，科室，院区的下拉框
			$('#'+tabId+' #depGroupList_disStruc').parent().css('display','none');
			$('#'+tabId+' #depList_disStruc').parent().css('display','none');
			$('#'+tabId+' #wardList_disStruc').parent().css('display','none');
			//加载上级页面传递过来的科室信息
			$.getJSON(contextPath+'/DrgsIndexCal/findAllDepByHospCode.dhc',param,function(data){
				var data=data.data;
				var depCodeFirst = param.depCode;
				var optStr="";
				for (var i = 0; i < data.length; i++) {
					optStr = optStr + '<option value="' + data[i].DEPCODE +'" ';
					data[i].DEPCODE == depCodeFirst ? optStr = optStr +' selected="selected" ' : '';				
					optStr = optStr + '>' + data[i].DEPNAME + '</option>';
				}
				$("#" + tabId + " #depList_disStruc").append(optStr);
				$("#" + tabId + " #depList_disStruc").val(depCodeFirst);
				loadDisStructrueTable('null', depCodeFirst,'null');//页面刷新自动加载默认的第一个科室
			})
		}else{//如果是其他路径进来的则正常显示
			if(commonLayerConfig.length>=4){
				
				if(commonLayerConfig[0]=="1"){//科室组层次 
					//显示科室组区域
					$('#'+tabId+' #depGroupList_disStruc').parent().css('display','block');
					$('#'+tabId+' #depList_disStruc').parent().css('display','none');
					$('#'+tabId+' #wardList_disStruc').parent().css('display','none');
					
					//加载全院的科室组
					$.getJSON(contextPath+'/DrgsIndexCal/getDepGroupByCode.dhc',{'hospCode':param.hospCode},
						function(depGroups){
						var data = depGroups.data;
						var optStr = '<option value="hosp" selected="selected">' + "全院"+ '</option>';
						for (var i = 0; i < data.length; i++) {
								optStr = optStr + '<option value="' + data[i].CODE
										+ '">' + data[i].NAME + '</option>';
						}
						$("#" + tabId + " #depGroupList_disStruc").append(optStr);
					});
				}else if(commonLayerConfig[1]=="1"){//科室层次
					$('#'+tabId+' #depGroupList_disStruc').parent().css('display','none');
					$('#'+tabId+' #wardList_disStruc').parent().css('display','none');
					$('#'+tabId+' #depList_disStruc').parent().css('display','block');
					
					$.getJSON(contextPath+'/DrgsIndexCal/findAllDepByHospCode.dhc',param,function(deps){
						var data = deps.data;
						var optStr = '<option value="" selected="selected">' + "全院"+ '</option>';
						for (var i = 0; i < data.length; i++) {
							if (i == 0) {
								optStr = optStr + '<option value="' + data[i].DEPCODE
										+ '">' + data[i].DEPNAME
										+ '</option>';
							} else {
								optStr = optStr + '<option value="' + data[i].DEPCODE
										+ '">' + data[i].DEPNAME + '</option>';
							}
						}
						$("#" + tabId + " #depList_disStruc").append(optStr);
					});
				}
				
			}
			loadDisStructrueTable('null', 'null','null');//页面刷新自动加载全院			
		}
	}
	
});
//加载表格方法
function loadDisStructrueTable(depGroupCode, depCode,wardCode) {
	
	var timer=setTimeout(function(){
		if ($('#'+tabId+' #loading').css('display')=='block') {
			$('#'+tabId+' #loading').html('<font size="40" face="黑体"><i class="icon-exclamation-sign'
					+' icon-4x"></i><br>应该出问题了...刷新试试吧&nbsp;:)</font>')
		} 
	}, 200000);
	
	//病组表格数据填充
	var param = drgUI.setAjaxData(tabId);
	param.pardepgroupCode = depGroupCode; //科室组
	param.pardepCode = depCode;  //科室
	
	$.post(contextPath +"/DisStructureAnalysis/getStructureData.dhc",param).done(function(result){
		if(result.success){
			var data = result.data;
			if(data.length>0){
				//填充columns
				var columns=[
				             	{"data" : "MITYPE", "title":""},
				            	{"data": "CNT","title":"入组病例数",
				           			"render": function(obj, type, full) {
				           				if(full.CNT==0){
				           					return 0;
				           				}else{
				           					var type;
				           					if(full.ORDERNUM=="1"){
				           						type="thisPeriod";
				           					}else if(full.ORDERNUM=="2"){
				           						type="lastPeriod";
				           					}else if(full.ORDERNUM=="3"){
				           						type="onlyThisPeriod";
				           					}else{
				           						type="onlyLastPeriod";
				           					}
					           				return '<a href="javascript:loadDisStructureCase(\''+type+'\')">'
					           					+formatNumber(full.CNT,0)+'</a>';
				           				}
				           			}
				               	},
				               	{"data": "CMI","title":"CMI值",
				           			"render": function(obj, type, full) {
				           				return formatNumber(full.CMI,2) ;
				           			}
				               	},
				             	{"data": "DRGCNT","title":"DRGs组数",
				           			"render": function(obj, type, full) {
				           				if(full.DRGCNT==0){
				           					return 0 ;
				           				}else{
				           					var type;
				           					if(full.ORDERNUM=="1"){
				           						type="this";
				           					}else if(full.ORDERNUM=="2"){
				           						type="last";
				           					}else if(full.ORDERNUM=="3"){
				           						type="lastNo";
				           					}else{
				           						type="thisNo";
				           					}
				           					return '<a href="javascript:loadDisStructureDrg(\''+type+'\',\'all\',\'DRGs\')">'
				           					+formatNumber(full.DRGCNT,0)+'</a>';
				           				}
				           			}
				               	},
				               	{"data": "INDRGCNT","title":"理论内DRGs组数",
				           			"render": function(obj, type, full) {
				           				if(full.INDRGCNT==0){
				           					return 0 ;
				           				}else{
				           					var type;
				           					if(full.ORDERNUM=="1"){
				           						type="this";
				           					}else if(full.ORDERNUM=="2"){
				           						type="last";
				           					}else if(full.ORDERNUM=="3"){
				           						type="lastNo";
				           					}else{
				           						type="thisNo";
				           					}
				           					return '<a href="javascript:loadDisStructureDrg(\''+type+'\',\'in\',\'理论内DRGs\')">'
				           					+formatNumber(full.INDRGCNT,0)+'</a>';
				           				}
				           			}
				               	},
				             	{"data": "OUTDRGCNT","title":"理论外DRGs组数",
				           			"render": function(obj, type, full) {
				           				if(full.OUTDRGCNT==0){
				           					return 0 ;
				           				}else{
				           					var type;
				           					if(full.ORDERNUM=="1"){
				           						type="this";
				           					}else if(full.ORDERNUM=="2"){
				           						type="last";
				           					}else if(full.ORDERNUM=="3"){
				           						type="lastNo";
				           					}else{
				           						type="thisNo";
				           					}
				           					return '<a href="javascript:loadDisStructureDrg(\''+type+'\',\'out\',\'理论外DRGs\')">'
				           					+formatNumber(full.OUTDRGCNT,0)+'</a>';
				           				}
				           			}
				               	},
				               	{"data": "NOTINDRGCNT","title":"理论内未覆盖DRGs组数",
				           			"render": function(obj, type, full) {
				           				if(full.NOTINDRGCNT==0){
				           					return 0 ;
				           				}else{
				           					var type;
				           					if(full.ORDERNUM=="1"){
				           						type="this";
				           					}else if(full.ORDERNUM=="2"){
				           						type="last";
				           					}else if(full.ORDERNUM=="3"){
				           						type="lastNo";
				           					}else{
				           						type="thisNo";
				           					}
				           					return '<a href="javascript:loadDisStructureDrg(\''+type+'\',\'notin\',\'理论内未覆盖DRGs\')">'
				           					+formatNumber(full.NOTINDRGCNT,0)+'</a>';
				           				}
				           			}
				               	},
				               	{"data": "RATIO","title":"有效DRGs组数占比（%）",
				           			"render": function(obj, type, full) {
				           				return formatNumber(full.RATIO,2) ;
				           			}
				               	}
				           ];
				var paramsDataTables={searching:false,lengthChange:false,ordering:false,stateSave:true,info:false
						,paging:false,tableTitle:"",columnDefs:[{"orderable": false,"targets": [ 0 ] },{"targets":"_all","className":"textAlignSzy"}]};
				var table = $("#"+tabId+" #disStructureAnalysisTable").dataTableBasic(columns,data,paramsDataTables) ;
				
			}else{
				drgUI.warning('您选择的条件查无数据，请重新选择！');
			}
			
		}
		clearTimeout(timer); //加载成功清除定时器
	});
	
}

//科室组点击事件
function depGroupChange_disStruc(){
	var param = drgUI.setAjaxData(tabId);
	var depGroupCode=$("#"+tabId+" #depGroupList_disStruc").val();
	if(depGroupCode=='hosp'){
		$('#'+tabId+' #depList_disStruc').parent().css('display','none');
		$('#'+tabId+' #wardList_disStruc').parent().css('display','none');
		loadDisStructrueTable('null', 'null','null');
	}else{
		$('#'+tabId+' #wardList_disStruc').parent().css('display','block');
		if(roleCode == "科室"){
			var depGroupCode=$("#"+tabId+" #depGroupList_disStruc").val();
		}else{
			if(commonLayerConfig[0]=="1"){//科室组层次 
				$('#'+tabId+' #depList_disStruc').parent().css('display','none');
				$('#'+tabId+' #wardList_disStruc').parent().css('display','none');
			}else if(commonLayerConfig[1]=="1"){//科室层次
				$('#'+tabId+' #wardList_disStruc').parent().css('display','none');
			}
			if(commonLayerConfig[1]=="1"){
				$.getJSON(contextPath+'/Department/findAllDep.dhc',{'parDepGroupCode':depGroupCode},function(deps){
					$('#'+tabId+' #depList_disStruc').html('');
					$('#'+tabId+' #depList_disStruc').parent().css('display','block');
					var data = deps.data;
					var optStr = '<option value="" selected="selected">' + "全部"+ '</option>';
					if(data.length>0){
						for (var i = 0; i < data.length; i++) {
							if (i == 0) {
								optStr = optStr + '<option value="' + data[i].depCode
										+ '">' + data[i].depName
										+ '</option>';
							} else {
								optStr = optStr + '<option value="' + data[i].depCode
										+ '">' + data[i].depName + '</option>';
							}
						}
					}
					$("#" + tabId + " #depList_disStruc").append(optStr);
				});
			}
		}
		//加载表格
		loadDisStructrueTable(depGroupCode, 'null','null');
	}
}

//科室点击事件
function depChange_disStruc(){
	var param = drgUI.setAjaxData(tabId);
	var depCode=$("#"+tabId+" #depList_disStruc").val();
	$('#'+tabId+' #wardList_disStruc').parent().css('display','none');
	
	if(depCode=='hosp'){
		$('#'+tabId+' #wardList_disStruc').parent().css('display','none');
		loadDisStructrueTable('null', 'null','null');
	}else if(depCode==''){//切换至请选择的时候，加载科室组
		var depGroupCode=$("#"+tabId+" #depGroupList_disStruc").val();
		loadDisStructrueTable(depGroupCode, 'null','null');
	}else{
	//加载表格
	loadDisStructrueTable('null', depCode,'null');
	}
}

//病例数点击链接
function loadDisStructureCase(monitorType){
	var param = drgUI.setAjaxData(tabId);
	var depGroupCode=$("#"+tabId+" #depGroupList_disStruc").val();
	var depCode=$("#"+tabId+" #depList_disStruc").val();
	var wardCode="";
	param.depGroupCode = depGroupCode;
	param.depCode = depCode;
	param.monitorType = monitorType;
	$.post(contextPath+'/DisGroupAnalysis/loadCase.dhc',param).done(function(result){
		if(depCode){
			depCode = depCode.replace(/,/g, '');
		}
		openTab('/pages/yizheng/fenxi/caselist.jsp', '病例明细'
				,monitorType+depGroupCode+depCode+wardCode+"disStructurecaselist"
				,monitorType+depGroupCode+depCode+wardCode+"disStructurecaselist"
				,{param:param, data:result.data});
	});
}

//病组数点击链接添加
//queryType是 比较时间类型 this本期 last同期 thisNo仅同期 lastNo仅本期
//monitorType是 类型比较  all:DRGs组数   in:理论内DRG组数 out:理论外DRG组数 notin:理论内未覆盖DRGs组数
function loadDisStructureDrg(queryType, monitorType,title){
	var param = drgUI.setAjaxData(tabId);//获取当前页面 初始参数
	param.title = title + ' 病组详情';
	param.queryType = queryType;
	param.monitorType = monitorType;
	param.depGroupCode = $("#"+tabId+" #depGroupList_disStruc").val();
	param.depCode = $("#"+tabId+" #depList_disStruc").val();
	param.wardCode = "";
	openTab('/pages/yibao/fenxi/disStructureDetail.jsp', '病组详情'
			,"disStructureAnalysis_disStructureDetail","disStructureAnalysis_disStructureDetail",param);
	
}
